package com.dtsz.autoconfigure.framework.redis.repository;

import com.dtsz.autoconfigure.framework.redis.domain.IRedisType;
import com.dtsz.autoconfigure.framework.redis.utils.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

public abstract class AbstractRedisRepository<T extends IRedisType> implements IRedisRepository {

    protected final String getKey(T type, String keySuffix) {
        this.validType(type);
        String keyDelimiter = this.getKeyDelimiter();
        String key = RedisKeyUtil.concatKey(type.getType(), keyDelimiter, type.getValue());
        if (StringUtils.isBlank(keySuffix)) {
            return key;
        }
        return RedisKeyUtil.concatKey(key, keyDelimiter, keySuffix);
    }

    protected void validType(T type) {
        Assert.isTrue(type != null && !StringUtils.isAnyBlank(type.getType(), type.getValue()),
                "This redis type and value is required; it must not be null");
    }
}
